package com.lee.mao;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lee.mao.adapter.TypeAdapter;
import com.lee.mao.base.BaseActivity;
import com.lee.mao.base.Mapplication;
import com.lee.mao.bean.ClassifyTable;
import com.lee.mao.bean.TypeValue;
import com.lee.mao.database.utils.ClassifyTableUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    private TabLayout tablayout;
    private ViewPager viewpager;
    private List<View> viewList;
    private List<ClassifyTable> allClassify;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ClassifyTableUtil instance = ClassifyTableUtil.getInstance((Mapplication) getApplication());


        boolean isFirst = sp.getBoolean("isFirst", true);
        if (isFirst) {
            initDataBaseInfo();
            sp.edit().putBoolean("isFirst", false).apply();
        }

        allClassify = instance.getAllClassify();

        initView();
        initData();


//        PayTableUtil.getInstance((Mapplication) getApplication()).queryAll();

    }


    private void initView() {
//        etToday = findViewById(R.id.etToday);
        tablayout = findViewById(R.id.tab);
        viewpager = findViewById(R.id.viewpager);

        for (ClassifyTable classifyTable : allClassify) {
            tablayout.addTab(tablayout.newTab().setText(classifyTable.getName()));
        }

        tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab被选的时候回调
                viewpager.setCurrentItem(tab.getPosition(), true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab未被选择的时候回调
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //tab重新选择的时候回调
            }
        });
        //2.ViewPager滑动关联tabLayout,如果不设置的话，当tab超出屏幕宽度时候，从右向左滑动，tab不会自动向左滚动
        viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));

//        hideShadow();

        // 将要分页显示的View装入数组中
        viewList = new ArrayList<>();

        LayoutInflater lf = getLayoutInflater().from(this);
        for (ClassifyTable classifyTable : allClassify) {

            if (classifyTable.getType() == TypeValue.CLASSIFY_TYPE_EDIT)
                viewList.add(lf.inflate(R.layout.layout_every, null));
            else if (classifyTable.getType() == TypeValue.CLASSIFY_TYPE_BOOKKEEPING)
                viewList.add(lf.inflate(R.layout.layout_bookkeeping, null));
        }

        TypeAdapter tabAdapter = new TypeAdapter(this, viewList, allClassify);

//        //设置tabLayout的标签来自于PagerAdapter
//        tablayout.setTabsFromPagerAdapter(tabAdapter);
        //viewpager设置适配器
        viewpager.setAdapter(tabAdapter);


    }

//    private void hideShadow() {
//        try {
//            Field mRecyclerView =  viewpager.getClass().getDeclaredField("mRecyclerView");
//            mRecyclerView.setAccessible(true);
//            RecyclerView recyclerView = (RecyclerView) mRecyclerView.get(viewpager);
//            recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
//        } catch (Exception ignore) {
//
//        }
//
//    }

    private void initData() {

        if (viewList == null || viewList.size() == 0)
            return;

        for (int i = 0; i < viewList.size(); i++) {
            View view = viewList.get(i);

            EditText c = view.findViewById(R.id.content);
            if (i == 0) {

                c.setText(sp.getString("today", ""));

            }

//            else if (i == 1) {
//                c.setText(sp.getString("week", ""));
//
//            } else if (i == 2) {
//                c.setText(sp.getString("month", ""));
//
//            }
        }

    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "onSaveInstanceState is invoke!!!");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG, "onRestoreInstanceState is invoke!!!");
    }


    private void initDataBaseInfo() {

        ClassifyTableUtil instance = ClassifyTableUtil.getInstance((Mapplication) getApplication());

        ClassifyTable c1 = new ClassifyTable("当日必做", TypeValue.CLASSIFY_TYPE_EDIT);
//        ClassifyTable c2 = new ClassifyTable("当周必做", TypeValue.CLASSIFY_TYPE_EDIT);
//        ClassifyTable c3 = new ClassifyTable("当月必做", TypeValue.CLASSIFY_TYPE_EDIT);
//        ClassifyTable c4 = new ClassifyTable("卡", TypeValue.CLASSIFY_TYPE_EDIT);
        ClassifyTable c5 = new ClassifyTable("支出", TypeValue.CLASSIFY_TYPE_BOOKKEEPING);
        ClassifyTable c6 = new ClassifyTable("收入", TypeValue.CLASSIFY_TYPE_BOOKKEEPING);

        try {
            instance.addClassify(c1);
//            instance.addClassify(c2);
//            instance.addClassify(c3);
//            instance.addClassify(c4);
            instance.addClassify(c5);
            instance.addClassify(c6);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }

}